<?php
// 广告主数据管理
namespace Admin\Controller;
use Think\Controller;
class AdvertiserDataController extends Controller
{

	public function __construct($value='')
	{
	 
		$this->isAjaxLogin();
	}
	// 数据列表
	public function dataList($value='')
	{
		$page=I('get.page',1);
		$page_size=I('get.page_size');

		$adv_id=I('get.adv_id/d');//广告主
		$ad_id=I('get.ad_id/d');//广告
		$start_time=I('get.start_time/s');//开始时间
		$end_time=I('get.end_time/s');//结束时间
		$input_status=I('get.input_status/s');//录入状态
		$settlement_status=I('get.settlement_status/s');//下发状态

		if (C('IS_FALSE_DEL')) {
			$where['is_delete']='0';
		}else{
			$where=[];
		}

		if (!empty($adv_id)&&($adv_id>-1)) {
			$where['adv_id']=$adv_id;
		}

		if (!empty($ad_id)&&($adv_id>-1)) {
			$where['ad_id'] = $ad_id;	 
		}

		if (in_array($input_status,['0','1'])) {
			$where['input_status']=$input_status;	 
		}

		if (in_array($settlement_status,['0','1'])) {
			$where['settlement_status']=$settlement_status;	 
		}

		if (!empty($start_time)&&!empty($end_time)) {

        	$where['add_time']  = [['egt',$start_time-8*3600],['elt',$end_time+16*3600]]; 
        }
        
		$model=M('advertiser_data')->field('is_delete,consumption_amount',true)->where($where);
		$arr=my_page($model,$page,$page_size);

		// if (!$arr['code']) {
		// 	$result['msg']='暂无数据';
		// 	$result['code']=500;
		// 	$result['data']['total_page']=$arr['count'];
		// 	$result['data']['list']=$data;
		// 	$this->ajaxReturn($result);
		// }
 

		$data=array_map(function($val){
			if ($val['cooperation_type']=='cpa') {
				$val['data']=$val['num'];	 
			}

			if ($val['cooperation_type']=='cps') {
				$val['data']=$val['amount'];	 		 
			}
			
			unset($val['amount']);
			unset($val['num']);

			$val['add_time']=date("Y-m-d",$val['add_time']);                   
			return $val;
		},$arr['data']);
	 
		$result['msg']='查询成功';
		$result['code']=200;
		$result['data']['total_page']=$arr['count'];
		$result['data']['list']=$data;
	 
		$this->ajaxReturn($result);
	}

	// 广告主数据录入     
	// 验证cps边界问题（新增）6.9 11:37
	public function dataInput($value='')
	{
		$model=M('advertiser_data');
		 
		$id=I('post.id/d');
		$data=I('post.data');

		$settlement_status=M('advertiser_data')->where(['id'=>$id])->getField('settlement_status');
		if ($settlement_status=='1') {
			$result['msg']='已下发不能录入';	
			$result['code']=501;
			$this->ajaxReturn($result);	
		}

		$ad_data=$model->find($id);
		if ($ad_data['cooperation_type']=='cpa') {
	 		
		 	
			$model->num=$data;//数量
		 
			$model->access_unit_price;
	 
			$res=$model->consumption_amount=$data*$model->access_unit_price;//总金额
			$res=$model->save();
		}

		if ($ad_data['cooperation_type']=='cps') {

			$where['start_flow']=array('elt',$data);
			$where['end_flow']=array('egt',$data);
			$where['ad_id']=$ad_data['ad_id'];
			 

			$divide=M('cps_rule')->where($where)->getField('divide');
			 
			if (empty($divide)) {
				$result['code']=502;
				$result['msg']='数据超出cps规则范围,太大或太小';
				$this->ajaxReturn($result);
			}

			$model->amount=$data;//流水
			$model->divide=$divide;//分成比例

			
			$model->consumption_amount=round(($divide/100)*$data,2);//乘 总金额
			$res=$model->save();
			
		}

		 

		if ($res) {
 
			$res=M('advertiser_data')->where(['id'=>$id])->setField('input_status','1');
			$result['code']=200;
			$result['msg']='录入成功';
		}else{
			$result['code']=500;
			$result['msg']='录入失败';
		}

		$this->ajaxReturn($result);

	}
	 
	// 广告主数据下发
	public function dataIssued($value='')
	{
		$id=I('get.id');
		$Advertiser=M('advertiser');
	 	$Advertiser_data=M('advertiser_data');
  		// 消耗金额
		$data=$Advertiser_data
		->where(['id'=>$id])
		->field('consumption_amount,adv_id,input_status,settlement_status')
		->find();

		if ($data['input_status']=='0') {
			$result['msg']='数据未录入,不能下发';
			$result['code']=501;
			$this->ajaxReturn($result);
		}

		if ($data['settlement_status']=='1') {
			$result['msg']='已下发,不能重复下发';
			$result['code']=502;
			$this->ajaxReturn($result);
		}

		M()->startTrans(); 

		$before_balance=$Advertiser->where(['id'=>$data['adv_id']])->getField('balance');//下发前余额
  		$res1=$Advertiser->where(['id'=>$data['adv_id']])->setDec('balance',$data['consumption_amount']); //扣除余额
  
  		$balance=$Advertiser->where(['id'=>$data['adv_id']])->getField('balance');//下发后余额

  		$res2=$Advertiser_data->where(['id'=>$id])->save(['settlement_status'=>'1','balance'=>$balance,'before_balance'=>$before_balance]);

  		if ($res1&&$res2) {
  			M()->commit();
  			$result['code']=200;
  			$result['msg']='下发成功';		
  		}else{	
  			M()->rollback();
  			$result['code']=500;
  			$result['msg']='下发失败';
  		}

		$this->ajaxReturn($result);

	}

	// 广告主消耗记录
	public function consumeRecord($value='')
	{
		$page=I('get.page',1);
		$page_size=I('get.page_size');

		$adv_id=I('get.adv_id/d');//广告主
		$ad_id=I('get.ad_id/d');//广告
		$start_time=I('get.start_time/s');//开始时间
		$end_time=I('get.end_time/s');//结束时间
		 
		if (C('IS_FALSE_DEL')) {
			$where['is_delete']='0';
		}else{
			$where=[];
		}

		if (!empty($adv_id)&&($adv_id>-1)) {
			$where['adv_id']=$adv_id;
		}

		if (!empty($ad_id)&&($ad_id>-1)) {
			$where['ad_id'] = $ad_id;	 
		}

		if (!empty($start_time)&&!empty($end_time)) {

        	$where['add_time']  = [['egt',$start_time-8*3600],['elt',$end_time+16*3600]]; 
        }

		$model=M('advertiser_data')->field('id,add_time,ad_name,num,amount,cooperation_type,consumption_amount,balance,before_balance')->where($where)->where(['settlement_status'=>'1']);
		$arr=my_page($model,$page,$page_size);

	 
		

		$data=array_map(function($val){
			if ($val['cooperation_type']=='cpa') {
				$val['data']=$val['num'];	 
			}

			if ($val['cooperation_type']=='cps') {
				$val['data']=$val['amount'];	 		 
			}
			unset($val['amount']);
			unset($val['num']);

			$val['add_time']=date("Y-m-d H:i:s",$val['add_time']);                   
			return $val;
		},$arr['data']);
		$result['data']['total_page']=$arr['count'];
		$result['data']['list']=$data;

		// if (!$arr['code']) {
		// 	$result['msg']='暂无数据';
		// 	$result['code']=500;	 
		// }else{
		 
			$result['msg']='查询成功';
			$result['code']=200;		
		// }
 
			 
	 
		$this->ajaxReturn($result);
	}




}